<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">供应商分类</p>
    </div>
    <div class="view-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:supplierSort:add']"
          >新建</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-table v-if="refreshTable" v-loading="loading" :data="Supplierlist" row-key="id"
            :default-expand-all="isExpandAll"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column prop="sortName" label="分类名称">
              <template slot-scope="scope">
                <span @click="handleItem(scope.row)" style="cursor:pointer">{{ scope.row.sortName }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="sortCode" label="分类编码" align="center"></el-table-column> -->
            <!-- <el-table-column prop="remark" label="备注" align="center"></el-table-column> -->
            <el-table-column label="操作" align="center" width="60">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.id !== -1"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleEdit(scope.row)"
                  v-hasPermi="['system:supplierSort:edit']"
                ></el-button>
                <el-button
                  v-if="scope.row.id !== 1 && scope.row.id !== -1"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:supplierSort:remove']"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="1">
          <div class="line"></div>
        </el-col>
        <el-col :span="15">
          <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item prop="searchValue">
              <el-input
                v-model="queryParams.searchValue"
                placeholder="名称或者编码模糊查询"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="searchSupplierList" @row-click="handleView" :default-sort="{prop: 'date', order: 'descending'}" border>
            <el-table-column label="序号" type="index" align="center" width="50">
              <template slot-scope="scope">
                <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
              </template>
            </el-table-column>
            <el-table-column label="编码" align="center" prop="supCode" :show-overflow-tooltip="true" width="150" sortable />
            <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="简称" align="center" prop="shortName"  :show-overflow-tooltip="true" />
            <el-table-column label="助记码" align="center" prop="mnemonicCode" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="供应商分类" align="center" prop="sortId" :show-overflow-tooltip="true" width="100" />
            <el-table-column label="纳税比例类型" align="center" :show-overflow-tooltip="true" width="120">
              <template slot-scope="scope">
                {{ scope.row.shareInvoice == null ? '' : scope.row.shareInvoice.taxpayerScale == '0' ? '13%' : scope.row.shareInvoice.taxpayerScale == '1' ? '9%' : scope.row.shareInvoice.taxpayerScale == '2' ? '6%' : scope.row.shareInvoice.taxpayerScale == '3' ? '3%' : scope.row.shareInvoice.taxpayerScale == '4' ? '1%' : '' }}
              </template>
            </el-table-column>
            <!-- <el-table-column label="营业执照编号" align="center" prop="businessLicense" :show-overflow-tooltip="true" width="150" /> -->
            <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" width="150" />
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="handleItem()"
          />
        </el-col>
      </el-row>
      <!-- 添加或编辑供应商分类对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
        <el-form ref="form" :model="form" :rules="rules" label-width="90px">
          <!-- <el-row>
            <el-col :span="24">
              <el-form-item label="分类编码" prop="sortCode">
                <el-input v-model="form.sortCode" placeholder="分类编码自动生成" disabled />
              </el-form-item>
            </el-col>
          </el-row> -->
          <el-row v-if="SupplierlistOptions.length > 0">
            <el-col :span="24">
              <el-form-item label="上级分类" prop="parentId">
                <treeselect v-model="form.parentId" :options="SupplierlistOptions" :normalizer="normalizer" placeholder="选择上级分类" noOptionsText="暂无数据" @select="changeSelect" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="分类名称" prop="sortName">
                <el-input v-model="form.sortName" placeholder="请输入分类名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" maxlength="50" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" class="btn-success mr20 width100" @click="submitForm">确 定</el-button>
          <el-button class="width100" @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listSupplierlist, querySupplierlist, detailSupplierlist, addSupplierlist, editSupplierlist, deleteSupplierlist } from "@/api/system/basedata/supplierlist/supplierlist"
import { listSupplier } from "@/api/system/basedata/supplier/supplier"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"

export default {
  name: "baseDataSupplierlist",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格树数据
      Supplierlist: [],
      // 部门树选项
      SupplierlistOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // parentId: [
        //   { required: true, message: "上级分类不能为空", trigger: "blur" }
        // ],
        sortName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ]
      },
      // 供应商列表
      searchSupplierList: [],
      // 查询参数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortId: undefined,
        searchValue: undefined
      }
    }
  },
  created() {
    this.getList()
    this.handleItem()
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listSupplierlist().then(res => {
        this.Supplierlist = this.handleTree(res.rows, "id")
        this.loading = false
      })
    },
    // 转换供应商分类数据结构 
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.sortName,
        children: node.children
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: undefined,
        sortName: undefined,
        layer: undefined,
        // sortCode: undefined,
        remark: undefined
      };
      this.resetForm("form")
    },
    // 新建按钮
    handleAdd(row) {
      this.reset()
      this.open = true
      this.title = "添加供应商分类"
      querySupplierlist().then(res => {
        this.SupplierlistOptions = this.handleTree(res.rows, "id")
      })
    },
    changeSelect(e){
      // console.log(e)
      // this.form.sortCode = e.sortCode
      this.form.layer = e.layer
    },
    // 展开/折叠
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    // 编辑按钮
    handleEdit(row) {
      this.reset()
      detailSupplierlist(row.id).then(res => {
        if(res.data.parentId == 0){
          res.data.parentId = undefined
          this.form = res.data
        }else{
          this.form = res.data
        }
        this.open = true
        this.title = "编辑供应商分类"
      })
      // if(row.id == 1){
      //   const list = { id: 0, sortName: '主分类', children: [] }
      //   this.SupplierlistOptions.push(list)
      // }else{
        querySupplierlist().then(res => {
          this.SupplierlistOptions = this.handleTree(res.rows, "id")
        })
      // }
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editSupplierlist(this.form).then(res => {
              this.$modal.msgSuccess("编辑成功")
              this.open = false
              this.getList()
            });
          } else {
            addSupplierlist(this.form).then(res => {
              this.$modal.msgSuccess("新建成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 删除按钮
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.sortName + '"的数据项？').then(() => {
        deleteSupplierlist(row.id).then(()=> {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
      }).catch(() => {})
    },
    // 根据分类查看供应商
    handleItem(item){
      if(item !== undefined){
        this.queryParams.sortId = item.id
      }
      listSupplier(this.addDateRange(this.queryParams)).then(res => {
        this.searchSupplierList = res.rows
        this.total = res.total
        this.getSupplierlist()
      })
    },
    getSupplierlist(){
      listSupplierlist().then(res => {
        this.searchSupplierList.forEach(item => {
          let arr = res.rows.find(e =>{
            return e.id == item.sortId
          })
          // console.log(arr)
          if(arr != undefined){
            item.sortId = arr.sortName
          }
        })
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.handleItem()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 查看详情
    handleView(row) {
      this.$router.push({path:"/basedata/supplier/detail",query:{id: row.id}})
    }
  }
}
</script>
